package com.gitee.wsl.css.selector.builder

import com.gitee.wsl.css.components.PageInterface
import com.gitee.wsl.css.components.ViewInterface
import com.gitee.wsl.css.rule.RuleSet
import com.gitee.wsl.css.selector.EmptySelector

fun ViewInterface.style(pageInterface: PageInterface, rules: RuleSet.() -> Unit): RuleSet {
    val testRuleSet = RuleSet(pageInterface, EmptySelector()).apply { rules() }
    val hashCode = testRuleSet.hashCode().toString()
    val ruleSet = namedStyles.getOrElse(hashCode) {
        val newRuleSet = pageInterface.classRuleSet(null, rules)
        namedStyles[hashCode] = newRuleSet
        newRuleSet
    }
    addRuleSetToView(ruleSet)
    return ruleSet
}

fun ViewInterface.style(pageInterface: PageInterface,name: String, rules: RuleSet.() -> Unit): RuleSet {
    val ruleSet = namedStyles.getOrElse(name) {
        val newRuleSet = pageInterface.classRuleSet(name, rules)
        namedStyles[name] = newRuleSet
        newRuleSet
    }
    addRuleSetToView(ruleSet)
    return ruleSet
}